/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 * 
 * If you have edited the file, please update
 * all the last-modified fields manually. Thanks!
 *
 * @last-modified by: 
 * @last-modified date: 
 * @last-modified time: 
 *
 * @author Swee Zhi
 * @date-created Sep 28, 2011    @time-created 5:20:17 PM
 *
 * @project: 
 * @module: 
 * @institution: 
 *
 */
package util.mailhelper;

import java.util.Date;
import java.util.Properties;
import javax.ejb.EJB;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import session.stateless.SystemMessagesBean;
import util.localization.WebLocalization;

public class EmailCreator {
    @EJB
    private SystemMessagesBean systemMessages;
    private WebLocalization localization;
    private String preferredLocale;
    
    private String email;
    private String emailServerName;
    private String emailFromAddress;
    private String toEmailAddress;
    private String mailer;

    public EmailCreator() {
        /* LOCALIZATION */
        localization = new WebLocalization();
        preferredLocale = localization.getPreferredLocaleFromSession();
        /* END LOCALIZATION */
        
        emailServerName = "smtp.gmail.com";
        emailFromAddress = "ADMIN<auhiew@gmail.com>";
        toEmailAddress = email;
        mailer = "JavaMailer";
    }
    
    public void sendEmail() {
        try {
            Properties props = new Properties();
            //props.put("mail.smtp.host", "smtp.gmail.com");
            props.put("mail.smtp.host", emailServerName);
            props.put("mail.smtp.socketFactory.port", "465");
            props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.port", "465");
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.debug", "true");
            javax.mail.Authenticator auth = new SMTPAuthenticator();

            Session session = Session.getInstance(props, auth);
            session.setDebug(true);
            Message msg = new MimeMessage(session);
            if (msg != null) {
                msg.setFrom(InternetAddress.parse(emailFromAddress, false)[0]);
                msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmailAddress, false));
                msg.setSubject("Forgot Password");
                // TODO: localize

                String passwordMsg1 = systemMessages.getSystemMessage("PASSWORD_MSG_1", preferredLocale);
                String passwordMsg2 = systemMessages.getSystemMessage("PASSWORD_MSG_2", preferredLocale);
                String passwordMsg3 = systemMessages.getSystemMessage("PASSWORD_MSG_3", preferredLocale);
//                String messageText = passwordMsg1 + name + ",\n\n" + passwordMsg2 + "\n" + password + "\n\n" + passwordMsg3;
//                msg.setText(messageText);
                msg.setHeader("X-Mailer", mailer);
                Date timeStamp = new Date();
                msg.setSentDate(timeStamp);
                Transport.send(msg);
            }
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
